var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl"
        }
    }
};
var ztree;

var vm = new Vue({
    el:'#rrapp',
    data:{
        parentList:[],
        showList: true,
        title: null,
        dept:{
            name:'',
            parentName:null,
            parentId:0,
            orderNum:0,
            dataKey:'',
            dataDesc:''
        },
        isAlert:false
    },
    methods: {
        getDept: function(_id){
            //加载部门树
            $.get(baseURL + "dataauth/findAllFirstDataAuth", function(r){
                if(r.code==0){
                    var _html ='';
                    for(var i=0;i<r.result.length;i++){
                        _html = _html + '<option value="'+r.result[i].id+'">'+r.result[i].dataName+'</option>'
                    }
                    $('#parentList').html(_html);

                    $('#parentList').selectpicker('refresh');
                    if(_id){
                        $('#parentList').selectpicker('val', _id).trigger("change");
                    }
                    console.log($('#parentList').val(),'ssssssssssssssssss')
                }
            })
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.dept = {
                name:'',
                parentName:null,
                parentId:0,
                orderNum:0,
                dataKey:'',
                dataDesc:''
            };
            $('#parentBox').show()
            vm.getDept();
        },
        update: function () {
            var deptId = getDeptId();
            if(deptId == null){
                return ;
            }
            $.ajax({
                type: "POST",
                url: baseURL + "dataauth/findDataAuthInfoById",
                dataType: "json",
                contentType : "application/json",
                data: JSON.stringify({
                    id:deptId
                }),
                success: function (r) {
                    vm.showList = false;
                    vm.title = "修改";
                    // vm.dept = r.dept;
                    vm.dept = {
                        deptId:r.result.id,
                        name:r.result.dataName,
                        orderNum:r.result.sort,
                        dataKey:r.result.dataKey,
                        dataDesc:r.result.dataDesc
                    };
                    vm.getDept(r.result.parentId);
                    $('#parentBox').hide()
                }
            });

        },
        del: function () {
            var deptId = getDeptId();
            if(deptId == null){
                return ;
            }

            confirm('确定要删除选中的记录？', function(){
                if(!vm.isAlert) {
                    vm.isAlert = true;
                    $.ajax({
                        type: "POST",
                        url: baseURL + "dataauth/deleteDataAuth",
                        contentType: "application/json",
                        data: JSON.stringify({
                            'id':deptId

                        }),
                        success: function (r) {
                            vm.isAlert = false;
                            if (r.code === 0) {
                                alert('操作成功', function () {
                                    vm.reload();
                                });
                            } else {
                                alert(r.msg);
                            }
                        }
                    });
                }
            });
        },
        saveOrUpdate: function (event) {
            $('#btnconfirm').attr('disabled', 'disabled');
            var url = vm.dept.deptId == null ? "dataauth/addDataAuth" : "dataauth/updateDataAuthInfo";
            var _data = {
                id:vm.dept.deptId,
                dataName:vm.dept.name,
                parentId:$('#parentList').val()?$('#parentList').val():0,
                dataKey:vm.dept.dataKey,
                dataDesc:vm.dept.dataDesc,
                sort:vm.dept.orderNum
            }
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(_data),
                success: function(r){
                    $('#btnconfirm').removeAttr('disabled');
                    if(r.code === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                },error:function(){
                    $('#btnconfirm').removeAttr('disabled');
                }
            });
        },
        deptTree: function(){
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择部门",
                maxmin : true,
                area: ['380px', '380px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#deptLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    //选择上级部门
                    vm.dept.parentId = node[0].deptId;
                    vm.dept.parentName = node[0].name;

                    layer.close(index);
                }
            });
        },
        reload: function () {
            vm.showList = true;
            Dept.table.refresh();
        }
    }
});

var Dept = {
    id: "deptTable",
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Dept.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: '数据权限ID', field: 'id', visible: false, align: 'center', valign: 'middle', width: '80px'},
        {title: '名称', field: 'dataName', align: 'center', valign: 'middle', sortable: true, width: '180px'},
        {title: '上级菜单', field: 'parentName', align: 'center', valign: 'middle', sortable: true, width: '100px'},
        {title: 'Key', field: 'dataKey', align: 'center', valign: 'middle', sortable: true, width: '100px'},
        {title: '排序号', field: 'sort', align: 'center', valign: 'middle', sortable: true, width: '100px'},
        {title: '数据描述', field: 'dataDesc', align: 'center', valign: 'middle', sortable: true, width: '100px',
            formatter:function (value,rowData,rowIndex) {
                var e = value.dataDesc? '<div class="poptipBox" data-toggle="tooltip" data-placement="top" title="'+value.dataDesc+'">'+value.dataDesc+'</div>':
                    '<div></div>';
                setTimeout(function () {
                   // $('[data-toggle="tooltip"]').tooltip();
                },0);
                return e;
            }
        }
        ];
    return columns;
};


function getDeptId () {
    var selected = $('#deptTable').bootstrapTreeTable('getSelections');
    if (selected.length == 0) {
        alert("请选择一条记录");
        return null;
    } else {
        return selected[0].id;
    }
}



$(function () {
    // $.get(baseURL + "sys/dept/info", function(r){
        var colunms = Dept.initColumn();
        var table = new TreeTable(Dept.id, baseURL + "dataauth/findDataAuthPage", colunms);

        table.setExpandColumn(2);
        table.setIdField("id");
        table.setCodeField("id");
        table.setParentCodeField("parentId");
        table.setExpandAll(false);
        table.init();
         Dept.table = table;

     // });
});
